<template>
  <a-modal :open="open" width="1200px" :title="tableRow.lsh ? '修改检验申请单' : '新增检验申请单'" centered @cancel="onCancel" @ok="onOk">
    <PatientInfoHeader :patient-info="patientData" class="text-white !bg-primary" class-name="text-white" @change="onCaseSuccess()">
      <a-divider type="vertical" :style="{ height: '40px', margin: '0 16px' }" />
      <div class="flex items-center">
        <LabelValue class="" label="信息卡号" :width="160" :value="patientData?.brId" />
        <LabelValue class="" label="诊断" :width="160" :value="patientData?.zdmc" />
        <LabelValue class="" label="主管医生" :width="160" :value="patientData?.empName" />
        <LabelValue class="" label="科室" :width="160" :value="patientData?.ksmc" />
      </div>
    </PatientInfoHeader>
    <div class="flex mt-7">
      <div class="w-[550px] mx-4">
        <BaseTable
          column-code="000228"
          :data-source="tableData"
          :custom-row="onClickRow"
          row-key="categroyCode"
          :row-class-name="(record) => (record.categroyCode === categroyCode ? 'selected-row' : '')"
          :scroll="{ x: 400, y: 430 }"
          :height="380"
          :loading="loading"
        >
          <template #bodyCell="{ column, index }">
            <template v-if="column.dataIndex == 'xh'">
              {{ index + 1 }}
            </template>
          </template>
        </BaseTable>
      </div>
      <div class="w-[560px]">
        <BaseTable
          column-code="000229"
          :data-source="projectData"
          row-key="orderItemCode"
          :scroll="{ x: 400, y: 430 }"
          :height="380"
          :loading="loadingProject"
          :row-selection="rowSelection"
        />
      </div>
    </div>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const categroyCode = ref('')
const categroyRow = ref({})
const defaultCheckedKeys = ref([])
const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  },
  tableRow: {
    type: Object,
    default: () => ({})
  },
  patientData: {
    type: Object,
    default: () => ({})
  }
})

const { loading, getData, tableData } = useTable({
  url: '/lis/InstrumentType/getLisCategroyList',
  formatResult: (data) => {
    categroyCode.value = data[0]?.categroyCode
    categroyRow.value = data ? data[0] : {}
    defaultCheckedKeys.value = []
    getProjectData()
    return data
  }
})

// lsh: props.tableRow.lsh
const {
  loading: loadingProject,
  getData: getProjectData,
  tableData: projectData
} = useTable({
  url: '/lis/LisApply/getlisInstrumentTypeRelationByCategroyCodeList',
  formatParams: () => {
    return {
      categroyCode: categroyCode.value,
      lsh: props.tableRow.lsh
    }
  },
  formatResult: (data) => {
    defaultCheckedKeys.value = data.map((i) => {
      if (i.chek === 1) return i.orderItemCode
    })
    return data
  }
})

const onClickRow = (record) => {
  return {
    onClick: () => {
      categroyCode.value = record.categroyCode
      categroyRow.value = record
      defaultCheckedKeys.value = []
      getProjectData()
    }
  }
}

const rowSelection = ref({
  checkStrictly: true,
  selectedRowKeys: defaultCheckedKeys,
  onChange: (selectedRowKeys) => {
    defaultCheckedKeys.value = selectedRowKeys
  }
})

const onOk = async () => {
  const { code, msg } = await post('/lis/LisApply/doSaveLisApply', {
    ...categroyRow.value,
    ...props.tableRow,
    orderItemCodeList: defaultCheckedKeys.value,
    keycode: props.rowData.jzbh,
    brId: props.tableRow.brId,
    // sqly: 0
    applyPath: 0,
    applyDeptCode: props.patientData?.ksdm
  })
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      getData()
    }
  }
)
</script>
